# 二叉树的锯齿形层序遍历

class Solution(object):
    def zigzagLevelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        result = []
        queue = [root]
        direction = 0
        while queue:
            size = len(queue)
            temp = []
            for _ in range(size):
                node = queue.pop(0)
                temp.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            if direction == 0:
                result.append(temp)
                direction = 1
            else:
                result.append(reversed(temp))
                direction = 0
        return result